package com.yingjia.yingjiauniversity.base;

import com.yingjia.yingjiauniversity.http.exception.BaseException;
import com.yingjia.yingjiauniversity.http.exception.ServerException;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by acer on 2018/6/19.
 */

public abstract class BaseObserver<T> implements Observer<BaseEntry<T>> {

    private CompositeDisposable compositeDisposable;

    public BaseObserver(CompositeDisposable compositeDisposable) {
        this.compositeDisposable = compositeDisposable;
    }

    @Override
    public void onSubscribe(Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(BaseEntry<T> tBaseEntry) {
        onSuc(tBaseEntry);
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof ServerException) {
            onError(new BaseException(e, ((ServerException) e).getMsg(), ((ServerException) e).getCode()));
        } else {
            onError(e);
        }
    }

    @Override
    public void onComplete() {

    }

    public abstract void onErr(BaseException baseException);

    public abstract void onSuc(BaseEntry<T> baseEntry);
}
